﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using Microsoft.VisualStudio.Text.Editor;
using System.Windows.Media;

namespace VsSpeakerCompanion.UI
{
    internal class SidebarPresenter : IWidget
    {
        private Sidebar view;

        private Context context;
        private IUIService uiService;

        LoginControlPresenter loginPresenter;
        WidgetsControlPresenter widgetsPresenter;

        EndingSessionControl endingSessionControl;

        internal SidebarPresenter(WidgetManager manager, Context context, IUIService uiService)
        {
            this.view = new Sidebar();
            this.view.PowerOff += new EventHandler(OnSidebarViewPowerOff);

            this.uiService = uiService;
            this.context = context;
            this.context.SessionStarted += new EventHandler(OnContextSessionStarted);
            this.context.SessionEnded += new EventHandler(OnContextSessionEnded);

            loginPresenter = new LoginControlPresenter(context, manager, uiService);
            widgetsPresenter = new WidgetsControlPresenter(this.context);
            
            endingSessionControl = new EndingSessionControl();
            endingSessionControl.Comitted += new EventHandler(OnEndingSessionControlComitted);
            endingSessionControl.Cancelled += new EventHandler(OnEndingSessionControlCancelled);

            this.view.SetContent(loginPresenter);
        }

        private void OnEndingSessionControlComitted(object sender, EventArgs e)
        {
            this.uiService.ExecuteOperation(() => this.context.EndSession());
            RestoreViewExpandStatusBeforeStartPoweringOff();
        }

        private void OnEndingSessionControlCancelled(object sender, EventArgs e)
        {
            this.view.SetContent(this.widgetsPresenter);
            RestoreViewExpandStatusBeforeStartPoweringOff();
        }

        private void RestoreViewExpandStatusBeforeStartPoweringOff()
        {
            if (!viewWasExpandedBeforeStartPoweringOff)
            {
                this.view.Collapse();
            }
        }

        private bool viewWasExpandedBeforeStartPoweringOff;

        private void OnSidebarViewPowerOff(object sender, EventArgs e)
        {
            this.view.SetContent(this.endingSessionControl);
            this.viewWasExpandedBeforeStartPoweringOff = this.view.IsExpanded;
            this.view.Expand();
            this.endingSessionControl.Start();
        }

        private void OnContextSessionStarted(object sender, EventArgs e)
        {
            this.view.UpdatePowerOnOffStatus(true);
            this.view.footer.Visibility = Visibility.Collapsed;
            this.view.SetContent(this.widgetsPresenter);
        }

        private void OnContextSessionEnded(object sender, EventArgs e)
        {
            this.view.UpdatePowerOnOffStatus(false);
            this.view.footer.Visibility = Visibility.Visible;
            this.view.SetContent(this.loginPresenter);
        }

        internal void Add(IWidget widget)
        {
            this.widgetsPresenter.Add(widget);
        }

        internal void Dock(IWpfTextView textView)
        {
            this.view.SetHeight(textView.VisualElement.ActualHeight);
            Canvas.SetLeft(this.view, textView.VisualElement.ActualWidth + textView.ViewportLeft - this.view.ActualWidth);
        }

        /// <summary>
        /// See <see cref="IWidget"/> for more information
        /// </summary>
        public System.Windows.UIElement VisualElement
        {
            get { return this.view; }
        }

        /// <summary>
        /// See <see cref="IWidget"/> for more information
        /// </summary>
        public void Reset()
        {
            this.widgetsPresenter.Reset();
        }
    }
}